package com.styles.codingstyle.controller;

import com.styles.codingstyle.Security.mapperAndInter.UserServiceForSecurity;
import com.styles.codingstyle.VO.CommonVO;
import com.styles.codingstyle.VO.UserVO;
import com.styles.codingstyle.dataobject.CacheTestEntity;
import com.styles.codingstyle.dataobject.UserEntity;
import com.styles.codingstyle.services.CacheTestService;
import com.styles.codingstyle.utils.CommonVO_Utils;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;

/**
 * @author chenjian
 * @Package
 * @Description: 开放权限主页controller
 * @date 2018/5/23 10:52
 */
@RestController
@RequestMapping(value = "/index")
//@Api(tags = "开放权限的资源")
public class IndexController {
    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private UserServiceForSecurity userServiceForSecurityImpl;

    @Autowired
    private CacheTestService cacheTestServiceImpl;

    @GetMapping(value = "/th")
    public Callable<String> threadTest() throws InterruptedException {
        logger.info("主线程开始");
        Callable<String> callable = new Callable<String>() {
            @Override
            public String call() throws Exception {
                logger.info("副线程开始");
                Thread.sleep(10000);
                logger.info("副线程结束");
                return "success";
            }
        };
        logger.info("主线程结束");
        return callable;
    }

    @GetMapping(value = "/cc")
    @ApiOperation(value = "方法的用途作用", notes = "方法备注")
   /* @ApiImplicitParams({
            @ApiImplicitParam(name = "mobile", value = "手机号", required = true, paramType = "form"),
            @ApiImplicitParam(name = "password", value = "密码", required = true, paramType = "form"),
            @ApiImplicitParam(name = "age", value = "年龄", required = true, paramType = "form", dataType = "Integer")
    })*/
    @ApiResponses({
            @ApiResponse(code = 400, message = "请求参数异常"),
            @ApiResponse(code = 404, message = "未找到相关资源")
    })
    public List<CacheTestEntity> findAll() {
        List<CacheTestEntity> list = cacheTestServiceImpl.findAll();
        return list;
    }

    @ApiOperation(value = "方法的用途作用", notes = "方法备注")
    @PostMapping(value = "/sw")
    public CommonVO swaggerApi(CacheTestEntity cacheTestEntity) {
        return CommonVO_Utils.success();
    }

    @GetMapping(value = "/aa")
    public void add() {
        cacheTestServiceImpl.add();
    }

    @GetMapping(value = "/regist")
    public ModelAndView toRegister() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/regist");
        return modelAndView;
    }

    /**
     * @Description: 接收注册信息
     * @Param: [objEntity, result]
     * @Return: com.styles.codingstyle.VO.CommonVO
     * @Author: 陈建
     * @date: 2018/6/11 14:03
     */
    @PostMapping(value = "/register")
    public CommonVO register(@Validated UserEntity userEntity, BindingResult result) {
        //处理参数验证信息
        ArrayList<String> list = new ArrayList<>();
        if (result.hasErrors()) {
            List<FieldError> fieldErrors = result.getFieldErrors();
            for (FieldError fieldError : fieldErrors) {
                String field = fieldError.getField();
                Object rejectedValue = fieldError.getRejectedValue();
                boolean bindingFailure = fieldError.isBindingFailure();
                Object[] arguments = fieldError.getArguments();
                String s = fieldError.getDefaultMessage();
                list.add(s);
            }
            System.out.println(list);
            return CommonVO_Utils.fail_404("error", list);
        }
        //保存数据(调用专用用户注册服务接口)
        UserVO userVO = userServiceForSecurityImpl.creatNewUser(userEntity);
        CommonVO commonVO = CommonVO_Utils.success("注册成功!", userVO);
        return commonVO;
    }

}
